package Franction;

public class Franction {
    int fenzi;
    int fenmu;

    public static void main(String[] args) {
        Franction[] f = Franction.toList("  1, 2, 1, 1, 0, 0, 430,3, 0, 2, 0, 1, 0, 460,1, 4, 0, 0, 0, 1, 420");
        System.out.println();

    }

    public Franction(int i, int j) {
        fenzi = i;
        fenmu = j;
        simplify();
        ZeroCheck();
    }

    public Franction(Franction f) {
        this(f.fenzi, f.fenmu);
    }

    public Franction() {
        fenzi = 0;
        fenmu = 0;
    }

    public Franction(String s) {
        String[] ss = s.split("/");
        if (ss.length == 1) {
            double d = Double.parseDouble(ss[0]);
            Copy(new Franction(d));
        } else {
            fenzi = Integer.parseInt(ss[0]);
            fenmu = Integer.parseInt(ss[1]);
        }
        simplify();
        ZeroCheck();
    }

    public Franction(int i) {
        this(i, 1);
    }

    public Franction(double d) {
        double t = d;
        int m = 1;
        while (t != (int) t) {
            t *= 10;
            m *= 10;
        }
        fenzi = (int) t;
        fenmu = m;
        simplify();
        ZeroCheck();
    }

    public boolean ZeroCheck() {
        if ((fenzi == 0) || (fenmu == 0)) {
            fenzi = 0;
            fenmu = 0;
            return false;
        } else
            return true;
    }

    public void simplify() {
        if (!ZeroCheck())
            return;
        int a = Math.max(Math.abs(fenzi), Math.abs(fenmu));
        int b = Math.min(Math.abs(fenzi), Math.abs(fenmu));
        while (b != 0) {
            int temp = a % b;
            a = b;
            b = temp;
        }
        fenzi /= a;
        fenmu /= a;
        if (fenzi * fenmu < 0) {
            fenzi = Math.abs(fenzi) * -1;
            fenmu = Math.abs(fenmu);
        } else {
            fenzi = Math.abs(fenzi);
            fenmu = Math.abs(fenmu);
        }
    }

    public void Copy(Franction f) {
        fenzi = f.fenzi;
        fenmu = f.fenmu;
    }

    public Franction add(Franction f) {
        if (ZeroCheck() && f.ZeroCheck()) {
            Franction t = new Franction((fenzi * f.fenmu + fenmu * f.fenzi), (fenmu * f.fenmu));
            return t;
        } else if (!ZeroCheck())
            return new Franction(f);
        else
            return new Franction(this);
    }

    public Franction add(int i) {
        return add(new Franction(i));
    }

    public Franction add(double d) {
        return add(new Franction(d));
    }

    public Franction opp() {
        return new Franction(fenzi * -1, fenmu);
    }

    public Franction minus(Franction f) {
        return add(f.opp());
    }

    public Franction minus(int i) {
        return minus(new Franction(i));
    }

    public Franction minus(double d) {
        return minus(new Franction(d));
    }

    public Franction multiply(Franction f) {
        if (ZeroCheck() && f.ZeroCheck()) {
            Franction t = new Franction((fenzi * f.fenzi), (fenmu * f.fenmu));
            return t;
        } else {
            return new Franction();
        }
    }

    public Franction multiply(int i) {
        if (i == 0)
            return new Franction();
        else
            return multiply(new Franction(i));
    }

    public Franction multiply(double d) {
        if (d == 0)
            return new Franction();
        else
            return multiply(new Franction(d));
    }

    public Franction rec() {
        return new Franction(fenmu, fenzi);
    }

    public Franction divide(Franction f) {
        return multiply(f.rec());
    }

    public Franction divide(int i) {
        if (i == 0)
            return new Franction();
        else
            return divide(new Franction(i));
    }

    public Franction divide(double d) {
        if (d == 0)
            return new Franction();
        else
            return divide(new Franction(d));
    }

    public String toString() {
        return "(" + fenzi + "/" + fenmu + ")";
    }

    public String toStrIO() {
        String str = "";
        if (!ZeroCheck())
            str += "0";
        else if (fenmu == 1 || fenmu == -1)
            str += "" + fenzi * fenmu;
        else
            str += "" + fenzi + "/" + fenmu;
        return str;
    }

    public int toInt() {
        return (int) toDouble();
    }

    public double toDouble() {
        if (!ZeroCheck())
            return 0.0;
        else
            return (double) fenzi / (double) fenmu;
    }

    public boolean isEqual(Franction f) {
        return !minus(f).ZeroCheck();
    }

    public boolean isEqual(double f) {
        return toDouble() == f;
    }

    public boolean isEqual(int f) {
        return toDouble() == f;
    }

    public boolean biggerthanZero() {
        if (!ZeroCheck())
            return false;
        else if (fenzi * fenmu > 0)
            return true;
        else
            return false;
    }

    public boolean isBigger(Franction f) {
        return minus(f).biggerthanZero();
    }

    public boolean isBigger(int i) {
        return minus(i).biggerthanZero();
    }

    public boolean isBigger(double d) {
        return minus(d).biggerthanZero();
    }

    public boolean isSmaller(Franction f) {
        return f.minus(this).biggerthanZero();
    }

    public boolean isSmaller(int i) {
        return toDouble() < i;
    }

    public boolean isSmaller(double d) {
        return toDouble() < d;
    }

    public static Franction[] toList(String str) {
        String[] s1 = str.split("[^0-9./-]+");
        if (s1[0].length() == 0) {
            String[] ss = new String[s1.length - 1];
            System.arraycopy(s1, 1, ss, 0, ss.length);
            s1 = ss;
        }
        Franction[] f = new Franction[s1.length];
        for (int i = 0; i < s1.length; i++) {
            f[i] = new Franction(s1[i]);
        }
        return f;
    }
}